//
//  MyDropsVC.swift
//  Drop
//
//  Created by zhuxuhong on 2018/3/19.
//  Copyright © 2018年 zhuxuhong. All rights reserved.
//

import UIKit
import Dialog

class MyDropCell: DropListCell {
	@IBOutlet weak var leftBtn: UIButton!
	@IBOutlet weak var leftBtnWidthConstraint: NSLayoutConstraint!
    
    override var drop: Drop!{
        didSet{
            super.drop = drop
            
            leftBtn.isUserInteractionEnabled = drop.isEditing
            let title: String? = drop.isEditing ? nil : "19日"
            let image: UIImage? = !drop.isEditing ? nil : UIImage.named("check_54")
			leftBtnWidthConstraint.constant = image == nil ? 34 : 30
			leftBtn.cornerRadius = leftBtnWidthConstraint.constant / 2
            leftBtn.setTitle(title, for: .normal)
            leftBtn.setImage(image, for: .normal)
            leftBtn.borderWidth = drop.isEditing ? 1 : 0
            leftBtn.borderColor = drop.isEditing
                ? .lightGray : .white
            leftBtn.tintColor = drop.isSelected ? .white : .lightGray
            leftBtn.backgroundColor  = drop.isEditing
                ? (drop.isSelected
                    ? .black : .white)
                : .white
        }
    }
}

class MyDropsVC: DropListVC {
    @IBOutlet weak var navEditBtn: UIButton!
    @IBOutlet weak var navSelectAllBtn: UIButton!
    @IBOutlet weak var navDeleteBtn: UIButton!
    
    fileprivate var isOperating = false
    
    override var drops: [Drop]!{
        didSet{
            title = "墨滴 (\(drops.count))"
            
            let bgColor: UIColor = isAllSelected ? .black : .clear
            let tintColor: UIColor = isAllSelected ? .white : .black
            navSelectAllBtn.backgroundColor = bgColor
            navSelectAllBtn.tintColor = tintColor
            
            navDeleteBtn.isHidden = selectedDrops.isEmpty
            navEditBtn.isHidden = drops.isEmpty
        }
    }
    
    fileprivate var selectedDrops: [Drop]{
        return drops.filter{ $0.isSelected }
    }
    
    fileprivate var isAllSelected: Bool{
        return selectedDrops.count == drops.count
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }

    @IBAction func navEdit(_ sender: UIButton){
        isOperating = !isOperating
        let icon = isOperating ? "check_54" : "edit"
        navEditBtn.setImage(UIImage.named(icon), for: .normal)
        navSelectAllBtn.isHidden = !isOperating
        navDeleteBtn.isHidden = !isOperating
        updateDropsForEachSelected(false)
    }
    
    @IBAction func navSelectAll(_ sender: UIButton){
        updateDropsForEachSelected(!isAllSelected)
    }
    
    fileprivate func updateDropsForEachSelected(_ isSelected: Bool) {
        drops.forEach{item in
            item.isEditing = self.isOperating
            item.isSelected = isSelected
        }
        drops = drops!
        tableView.reloadSectionsWithoutAnimation(.zero)
    }
    
    @IBAction func navDelete(_ sender: UIButton){
		let selects = selectedDrops
		
		func confirmDelete(){
			var rows: [Int] = []
			for item in selects{
				let index = drops.index(of: item)!
				rows.append(index)
			}
			for item in selects{
				let index = drops.index(of: item)!
				drops.remove(at: index)
			}
			
			if drops.isEmpty {
				tableView.deleteSections(.zero, with: .automatic)
			}
			else{
				tableView.deleteRows(at: rows.map{ IndexPath(row: $0, section: 0) }, with: .top)
			}
			navEdit(navEditBtn)
		}
		
		Dialog.default(title: .init("确定要删除 \(selects.count) 条墨滴?"), message: .init("删除后不可恢复"), actions:[
			.init(title: "删除", style: .destructive, handler: {_ in
				confirmDelete()
			}),
			.init(title: "取消", style: .cancel)
		])
    }
    
    @IBAction func cellSelect(_ sender: UIButton){
        if let cell = sender.superview?.superview as? MyDropCell,
            let indexPath = tableView.indexPath(for: cell) {
            let drop = drops[indexPath.row]
            drop.isSelected = !drop.isSelected
            cell.drop = drop
            drops = drops!
        }
    }
    
    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel()
        label.backgroundColor = .white
        
        let attr1 = NSAttributedString(string: "  3月", font: .boldSystemFont(ofSize: 30), lineHeightMultiple: 1)
        let attr2 = NSAttributedString(string: " / 2018年 (4条记录)", font: .systemFont(ofSize: 16), color: .gray, lineHeightMultiple: 1)
        label.attributedText = NSMutableAttributedString(attrStrings: [attr1, attr2])
        
        return label
    }
    
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 54
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 220
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return drops.isEmpty ? 0 : 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return drops.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueCell(withClass: MyDropCell.self, for: indexPath)!
        
        cell.drop = drops[indexPath.row]

        return cell
    }
}
